package com.bootdo.mall.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bootdo.common.excel.DachuExcelUtils;
import com.bootdo.common.utils.*;
import com.bootdo.haojiangboutils.MapBuild;
import com.bootdo.hotel.domain.HotelOrder;
import com.bootdo.mall.service.MallOrderLogService;
import com.bootdo.redis.RedisUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.mall.domain.MallOrder;
import com.bootdo.mall.service.MallOrderService;

/**
 * 订单记录
 * 
 * @author haojiangbo
 * @email 2100272728@qq.com
 * @date 2019-10-25 16:19:11
 */
 
@Controller
@RequestMapping("/mall/mallOrder")
public class MallOrderController extends BaseController{


	@Autowired
	private MallOrderService mallOrderService;
	@Autowired
	private MallOrderLogService mallOrderLogService;
	@Autowired
	private RedisUtil redisUtil;
	
	@ModelAttribute
	public MallOrder get(@RequestParam(required=false) Long id) {
		if (id != null){
			return mallOrderService.get(id);
		}else{
			return new MallOrder();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/mall/mallOrder','mall:mallOrder:mallOrder',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','mall:mallOrder:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("mall:mallOrder:mallOrder")	
	String MallOrder(MallOrder mallOrder,HttpServletRequest request,HttpServletResponse response){
	    return "mall/mallOrder/mallOrder";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("mall:mallOrder:mallOrder")
	public PageUtils list(@RequestParam Map<String, Object> params,HttpServletRequest request){
		//查询列表数据
        Query query = new Query(params);
        if(!isAdmin()){
        	//如果是物业 2 = 物业
        	if(getUser().getUserType().intValue() == 2){
				query.put("communityId", getUser().getCommunityId());
			}else {
				query.put("userId", getUserId());
			}
		}
        String sso_token =  request.getHeader("sso_token");
        String qq =  request.getParameter("qq");
        if(StringUtils.isNotEmpty(qq)){
			redisUtil.set(sso_token+"mall_order_query",params);
		}
		List<MallOrder> mallOrderList = mallOrderService.list(query);
		int total = mallOrderService.count(query);
		PageUtils pageUtils = new PageUtils(mallOrderList, total);
		if(StringUtils.isNotEmpty(qq)){
			Map map = new HashMap();
			query.put("type", 2);
			map.put("jf", mallOrderService.getTotalOrderPar(query));
			query.put("type", 1);
			map.put("wx", mallOrderService.getTotalOrderPar(query));
			pageUtils.setPaylod(map);
		}
		return pageUtils;
	}






	@ResponseBody
	@RequestMapping("/exportExcel")
	@RequiresPermissions("mall:mallOrder:mallOrder")
	public void exportExcel(HttpServletRequest request,HttpServletResponse response){
		String sso_token =  request.getParameter("sso_token");
		if(StringUtils.isEmpty(sso_token)){
			return;
		}else{
			Map query = (Map) redisUtil.get(sso_token+"mall_order_query");
			query.remove("offset");
			query.remove("limit");
			List<MallOrder>  mallOrders =  mallOrderService.list(query);
			try {
				new DachuExcelUtils<MallOrder>().exportExcle("订单统计-" + DateUtils.format(new Date()), MallOrder.class, mallOrders, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}



	@GetMapping("/add")
	@RequiresPermissions("mall:mallOrder:edit")
	String add(MallOrder mallOrder,HttpServletRequest request,HttpServletResponse response){
	    return "mall/mallOrder/mallOrderedit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(MallOrder mallOrder) {
		return R.ok().put("i",mallOrder);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("mall:mallOrder:edit")
	public R save( MallOrder mallOrder){
	
		if(mallOrderService.save(mallOrder)>0){
			return R.ok();
		}
		return R.error();
	}



	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/fahuo")
	@RequiresPermissions("mall:mallOrder:edit")
	public R fahuo(@RequestParam("orderId")String orderId,
				   @RequestParam("wuliuName")String wuliuName,
				   @RequestParam("wuliuOrderNo")String wuliuOrderNo
				   ){
		int r = mallOrderService.updatePayStatusByOrderId(orderId, 2, System.currentTimeMillis() + (1000 * 60 * 1), wuliuName, wuliuOrderNo, 1);
		if(r <= 0){
			return R.error("状态修改失败");
		}
		mallOrderService.updateFahuoTimeByOrderId(orderId,new Date());
		return R.ok().put("i", mallOrderService.getByOrderId(orderId));
	}



	/**
	 * 单商品退货
	 */
	@ResponseBody
	@PostMapping("/tuihuo")
	@RequiresPermissions("mall:mallOrder:edit")
	public R tuihuo(@RequestParam("id")Long id,
					@RequestParam("num")Integer num
	){
		try {
			String r =  mallOrderService.tuiHuoByOrderLogId(id, num);
			return R.ok(r);
		}catch (Exception e){
			e.printStackTrace();
			return R.error(e.getMessage());
		}

	}



	/*@ResponseBody
	@PostMapping("/jiesuan")
	@RequiresPermissions("mall:mallOrder:edit")
	public R jiesuan(@RequestParam("id")Long id ,@RequestParam("money") Double money){

		MallOrder mallOrder =  mallOrderService.get(id);
		if(null == mallOrder){
			return R.error("订单不存在");
		}
		int r = mallOrderService.updatePayStatus(id, 1, 0, money);
		if(r  == 0){
			return R.error("结算失败");
		}
		return R.ok("结算成功");
	}

	*//**
	 * 取消订单

	 * @return
	 */
	@ResponseBody
	@PostMapping("/tuidan")
	@RequiresPermissions("mall:mallOrder:edit")
	public R tuidan(@RequestParam("orderId")String orderId){
		String msg = "";
		try {
			msg =  mallOrderService.tuiHuoByOrderId(orderId);
		}catch (Exception e){
			e.printStackTrace();
			return R.error(e.getMessage());
		}
		return  R.ok(msg);
	}


	@ResponseBody
	@PostMapping("/wancheng")
	@RequiresPermissions("mall:mallOrder:edit")
	public R wancheng(@RequestParam("orderId")String orderId){
		List<MallOrder> list =    mallOrderService.list(MapBuild.getInstand().setKV("orderId", orderId));
		//如果配送成功的订单 大于 0 个
		try {
			String r =  mallOrderService.overOrderByOrderId(list);
			return  R.ok(r);
		}catch (Exception e){
			e.printStackTrace();
			return R.error(e.getMessage());
		}
	}


}